#!/bin/bash
build_type="Debug"
max_cycle=100000000
max_inst=-1
# trace=./trace/coremark.trace
trace=./trace/microbench-test.trace
config=./config/mcore.cfg
build() {
	mkdir -p ./build
	cd build
	cmake .. -DCMAKE_BUILD_TYPE=$build_type
	make -j32
	cd ..
}

clean() {
	rm -rf ./build
}

run() {
	echo ./build/msim -t$trace -i$max_inst -c$max_cycle $@
	./build/msim -t$trace -i$max_inst -c$max_cycle $@
}

all() {
	build
	run $@
}

perf_record() {
	build_type="FastDebug"
	build
	mkdir -p ./log
	sudo perf record -o ./log/perf.data ./build/msim -t$trace -i$max_inst -c$max_cycle $@
	sudo chmod +rw ./log/perf.data
	perf report -i ./log/perf.data > ./log/perf.txt
}

perf_stat() {
	build_type="FastDebug"
	build
	sudo perf stat ./build/msim -t$trace -i$max_inst -c$max_cycle $@
}

all_fast() {
	build_type="Release"
	build
	run $@
}

fun=$1
shift 1
case "$fun" in
	"clean")
		clean
		;;
	"build")
		build
		;;
	"run")
		run $@
		;;
	"all")
		all $@
		;;
	"perf_record")
		perf_record $@
		;;
	"perf_stat")
		perf_stat $@
		;;
	"all_fast")
		all_fast $@
		;;
	*)
		cat $config | grep -v "^#" | xargs ./build.sh all_fast $@
		;;
esac

